package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.AdvertMapper;
import com.ybkj.daijia.server.model.Advert;
import com.ybkj.daijia.server.service.AdvertService;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 */
@Service
public class DefaultAdvertService implements AdvertService {

    @Autowired
    private AdvertMapper advertMapper;

    public Page<Advert> queryAll() {

        Subject currentUser = SecurityUtils.getSubject();
        final Principal principal = (Principal) currentUser.getPrincipal();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        List<Advert> content = advertMapper.selectByPage(Pager.getOffset(), Pager.getLimit());
        int total = advertMapper.countAdvert();

        return new PageImpl<Advert>(content, pageable, total);

    }

    @Transactional(rollbackFor = Exception.class)
    public Advert save(Advert advert) {

        advertMapper.insertRecord(advert);

        return advert;

    }

    public Advert findById(Long id) {

        return advertMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(Advert advert) {

        return advertMapper.updateById(advert);
    }

    public void delete(Advert advert) {

        advertMapper.deleteByPrimaryKey(advert.getId());
    }

    public List<Advert> findAll() {

        return advertMapper.selectAllAdvert();
    }

}
